package com.brucekong.slm.controller.system;

import com.brucekong.slm.common.Result;
import com.brucekong.slm.model.system.SystemDomain;
import com.brucekong.slm.service.system.SystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统控制类
 */
@RestController
@RequestMapping("/system")
@Slf4j
@Api(value = "系统模块", tags = "系统模块")
public class SystemController {

    private Result result = new Result();

    @Autowired
    private SystemService systemService;

    /**
     * 获取系统菜单
     *
     * @return resultList 菜单树形结构列表
     */
    @GetMapping("/menu")
    @SuppressWarnings("unchecked")
    @ApiOperation(value = "菜单列表", notes = "获取菜单列表", code = 200, produces = "application/json", responseContainer = "List", response = Result.class)
    public Result getMenu() {
        log.info("/system/menu");
        List<SystemDomain> systemDomainList = systemService.getMenu();
        //最后返回的容器集合
        List<SystemDomain> resultList = new ArrayList<>();
        // 先找到所有的一级菜单
        for (SystemDomain systemDomain : systemDomainList) {
            // 一级菜单没有parentId
            if (systemDomain.getPcode().equals("0")) {
                systemDomain.setList(new ArrayList<>());
                resultList.add(systemDomain);
            }
        }
        // 为一级菜单设置子菜单，getChild是递归调用的
        for (SystemDomain menu : resultList) {
            List<SystemDomain> childList = getChild(menu.getId().toString(), systemDomainList);

            if (childList != null) {
                menu.setList(childList);
            } else {
                menu.setList(new ArrayList<>());
            }

        }

        result.setCode(0);
        result.setMsg(null);
        result.setSuccess(true);
        result.setData(resultList);
        return result;
    }

    /**
     * 递归查找子菜单
     *
     * @param id       当前菜单id
     * @param rootMenu 要查找的列表
     * @return childList
     * 返回子叶列表
     */
    private List<SystemDomain> getChild(String id, List<SystemDomain> rootMenu) {
        // 子菜单
        List<SystemDomain> childList = new ArrayList<>();
        for (SystemDomain menu : rootMenu) {
            // 遍历所有节点，将父菜单id与传过来的id比较
            if (menu.getPcode().equals(id)) {
                menu.setList(new ArrayList<>());
                childList.add(menu);
            }
        }
        // 把子菜单的子菜单再循环一遍
        for (SystemDomain menu : childList) {// 没有url子菜单还有子菜单
            if (menu.getHasChild().equals(1)) {
                // 递归
                menu.setList(getChild(menu.getId().toString(), rootMenu));
            }
        }
        // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }

}

